package com.sw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.sw.dao.BlockDao;
import com.sw.dao.TransactionDao;
import com.sw.entity.PageResult;
import com.sw.entity.QueryPageBean;
import com.sw.pojo.BlockInfo;
import com.sw.pojo.TransactionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import java.math.BigInteger;
import java.util.List;

/**
 * @program: monitor2
 * @description: 页面展示请求
 * @author: Wangzheng
 * @create: 2020-12-26 15:41
 **/
@RestController
@RequestMapping("/ExploreShow")
public class ExploreShow {
	@Autowired
	private BlockDao blockDao;
	@Autowired
	private TransactionDao transactionDao;
	
	//展示区块信息
	@RequestMapping("/showAllBlocks")
	public List<BlockInfo> findAll(){
		return blockDao.selectList(new QueryWrapper<>());
	}

	//展示交易信息
	@RequestMapping("/showAllTrades")
	public List<TransactionInfo> showAllTrades(){
		return transactionDao.selectList(new QueryWrapper<>());
	}
	
	//根据区块id查区块详情
	@RequestMapping("/findBlockDetailsByBlockNum/{blockNum}")
	public BlockInfo findBlockDetailsByBlockNum(@PathVariable("blockNum")BigInteger blockNum){
		return blockDao.selectById(blockNum);
	}

	//根据编号id查具体交易详情
	@RequestMapping("/findTransactionDetailsById/{id}")
	public TransactionInfo findTransactionDetailsById(@PathVariable("id")Integer id){
		return transactionDao.selectById(id);
	}
	
	//条件查询
	@RequestMapping("/findByPage")
	public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
		Integer currentPage = 1;
		Integer pageSize = 10;
		String queryString = queryPageBean.getQueryString();//查询条件
		//完成分页查询，基于mybatis框架提供的分页助手插件完成
		PageHelper.startPage(currentPage, pageSize);
		//select * from t_checkitem limit 0,10
		Page<BlockInfo> page =blockDao.selectByCondition(queryString);
		long total = page.getTotal();
		List<BlockInfo> rows = page.getResult();
		return new PageResult(total, rows);
	}
	
	
}